package com.mlm.mlmapp.adapter.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.util.Constants;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.activity.HairDresserDetailActivity_;
import com.mlm.mlmapp.adapter.view.listener.HairStyleClickListener;
import com.mlm.mlmapp.adapter.view.listener.OnMoreClickListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HairStylePublicAdapter extends
        RecyclerView.Adapter<RecyclerView.ViewHolder> implements HTTPCons {
    private AQuery aq;
    private static final int TYPER_PAGER = 0;
    private static final int TYPE_ITEM = 1;
    private static final int TYPE_DRESSER_TITLE = 2;
    private static final int TYPER_DRESSER_LIST = 3;
    private static final int TYPE_MORE = 4;
    private static final int HEADER_PAGER_LAYOUT = 3;

    private Context mContext;
    private JSONObject object;
    private HairStyleClickListener mOnHairStyleClickListener;
    private String id;
    private OnMoreClickListener l;
    private JSONArray photoArray;
    private String mainPhoto;
    private String intro;
    private String dresserName;
    private String dresserPost;
    private int dresserRating;
    private int dresserCollection;
    private String dresserHeadimg;
    private int dresserOrder;
    private String stylistID;
    private int isCollected;
    private String dresserIntro;
    private int price;
    private JSONObject coupon;
    private JSONArray stylistInfos;
    private TextView collectImageView;
    private int DRESSER_ITEM_NUM = 0;
    String hairurl;
    public static TextView temp;
    public HairStylePublicAdapter(Context context, JSONObject object, OnMoreClickListener l) {

        this.object = object;
        this.mContext = context;
        stylistInfos = CommonUtils.getJsonArray(object, "stylistInfos");
//        stylistInfos.remove(0);
//        DRESSER_ITEM_NUM = (stylistInfos.length() + 1) / 2;
        DRESSER_ITEM_NUM = stylistInfos.length();
        coupon = CommonUtils.getJsonObject(object, "coupon");
        intro = CommonUtils.getJsonString(object, "infoDescription");
        mainPhoto = CommonUtils.getJsonString(object, "mainPhoto");
        isCollected = CommonUtils.getJsonInt(object, "isCollect");
        photoArray = CommonUtils.getJsonArray(object, "photos");

        this.l = l;
        aq = new AQuery(mContext);

        mOnHairStyleClickListener = (HairStyleClickListener) mContext;

        DRESSER_LIST_POSITION_START = 1 + 1 + photoArray.length();

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {

        if (viewType == TYPER_PAGER) {

            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.hair_style, parent, false);
            final HeaderViewHolder headerHolder = new HeaderViewHolder(v);
            return headerHolder;
        } else if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.hairstyle_detail_viewitem, parent, false);
            return new RecyclerViewHolder(v);
        } else if (viewType == TYPE_DRESSER_TITLE) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.hairstyle_public_title_layout, parent, false);
            return new TitleViewHolder(v);
        } else if (viewType == TYPER_DRESSER_LIST) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.hairstyle_public_dressers_layout_item, parent, false);
            return new DreserListViewHolder(v);
        } else if (viewType == TYPE_MORE) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.hairstyle_public_more_layout, parent, false);
            return new MoreViewHolder(v);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder hHolder = (HeaderViewHolder) holder;
            hHolder.tv_style_intro.setText(this.intro);
            temp=hHolder.iv_collection;
            if (this.isCollected == 1) {
                hHolder.iv_collection.setBackground(mContext.getResources().getDrawable(R.drawable.style_detail_collect));
            } else {
                hHolder.iv_collection
                        .setBackground(mContext.getResources().getDrawable(R.drawable.style_detail_uncollect));
            }
            hHolder.iv_collection.setText(CommonUtils.getJsonString(object,"collectCount"));
            this.collectImageView = hHolder.iv_collection;
            aq.id(hHolder.iv_hairstyle)
                    .image(mainPhoto, true, true,
                            400, 0, null,
                            Constants.FADE_IN);

        } else if (holder instanceof RecyclerViewHolder) {

            RecyclerViewHolder rHolder = (RecyclerViewHolder) holder;

            if (photoArray.length() > 0) {
                try {
                    hairurl = CommonUtils.getJsonString(
                            photoArray.getJSONObject(position
                                    - 1), "photoUrl");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            aq.id(rHolder.iv_hairstyle).image(hairurl, true, true, 200, 0, null,
                    Constants.FADE_IN);

        } else if (holder instanceof DreserListViewHolder) {
            if (stylistInfos.length() > 0) {
                JSONObject stylist = getDresserObj(position);
                if (stylist == null) {
                    return;
                }
                DreserListViewHolder vholder = (DreserListViewHolder) holder;
                aq.id(vholder.iv_dresser).image(CommonUtils.getJsonString(stylist, "iconPhotoUrl"), true, true, 200, 0, null,
                        Constants.FADE_IN);
                final String id = CommonUtils.getJsonString(stylist, "id");
                vholder.tv_name.setText(CommonUtils.getJsonString(stylist, "name"));
                vholder.tv_post.setText(CommonUtils.getJsonString(stylist, "leveName"));
                vholder.tv_price.setText(mContext.getString(R.string.money, CommonUtils.getJsonInt(stylist, "priceMast")));
                vholder.tv_orders.setText(mContext.getString(R.string.txt_order, CommonUtils.getJsonInt(stylist, "bought")));
                vholder.ll_appointment.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mOnHairStyleClickListener
                                .onAppointmentClick(id);
                    }
                });
            }
        }

    }

    private JSONObject getDresserObj(int position) {
        JSONObject object1 = null;
        try {

            if (position >=DRESSER_LIST_POSITION_START) {
                object1 = stylistInfos.getJSONObject(position - DRESSER_LIST_POSITION_START);
                return object1;
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    private int DRESSER_LIST_POSITION_START;

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position)) {
            return TYPER_PAGER;
        }
        if (isStyleItem(position)) {
            return TYPE_ITEM;
        }
        if (isDresserItem(position)) {
            return TYPER_DRESSER_LIST;
        }
        if (isDresserTitleItem(position)) {
            return TYPE_DRESSER_TITLE;
        }
        if (isDresserMoreItem(position)) {
            return TYPE_MORE;
        }
        return -1;
    }

    public boolean isPositionHeader(int position) {
        return position == 0;
    }

    public boolean isStyleItem(int position) {
        if (position > 0 && position < photoArray.length() + 1) {
            return true;
        }
        return false;
    }

    public boolean isDresserItem(int position) {
        if (position > photoArray.length() + 1 && position < getItemCount() - 1) {
            return true;
        }
        return false;
    }

    public boolean isDresserTitleItem(int position) {
        if (position == photoArray.length() + 1) {
            return true;
        }
        return false;
    }

    public boolean isDresserMoreItem(int position) {
        if (position == getItemCount() - 1) {
            return true;
        }
        return false;
    }

    @Override
    public int getItemCount() {
        return photoArray.length() + HEADER_PAGER_LAYOUT + DRESSER_ITEM_NUM;
    }

    public void setCollectStatus(int status) {
        this.isCollected = status;
        if (status == 1) {
            this.collectImageView.setBackground(mContext.getResources().getDrawable(R.drawable.style_detail_collect));
        } else {
            this.collectImageView.setBackground(mContext.getResources().getDrawable(R.drawable.style_detail_uncollect));
        }

    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_hairstyle;
        ImageView iv_headimg;
        ImageView iv_share;
        Button iv_appointment;
        ImageView iv_back;
        TextView iv_collection;
        TextView tv_style_intro;
        TextView tv_name;
        TextView tv_post;
        TextView tv_words;
        TextView tv_collections;
        TextView tv_orders;
        TextView tv_type;
        TextView tv_price;
        LinearLayout dresser;
        // Button btn_coupon;
        // RatingBar ratingBar1;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            dresser = (LinearLayout) itemView
                    .findViewById(R.id.dresser);
            dresser.setVisibility(View.GONE);
            this.tv_style_intro = (TextView) itemView
                    .findViewById(R.id.tv_style_intro);
            this.tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            this.tv_post = (TextView) itemView.findViewById(R.id.tv_post);
            this.tv_words = (TextView) itemView.findViewById(R.id.tv_words);
            this.tv_collections = (TextView) itemView
                    .findViewById(R.id.tv_collections);
            this.tv_orders = (TextView) itemView.findViewById(R.id.tv_orders);
            this.tv_type = (TextView) itemView.findViewById(R.id.tv_type);
            this.tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            this.iv_hairstyle = (ImageView) itemView
                    .findViewById(R.id.iv_hairstyle);
            this.iv_headimg = (ImageView) itemView
                    .findViewById(R.id.iv_headimg);
            this.iv_share = (ImageView) itemView.findViewById(R.id.iv_share);
            this.iv_appointment = (Button) itemView
                    .findViewById(R.id.iv_appointment);
            this.iv_back = (ImageView) itemView.findViewById(R.id.iv_back);
            this.iv_collection = (TextView) itemView
                    .findViewById(R.id.iv_collection);
            // this.btn_coupon = (Button)
            // itemView.findViewById(R.id.btn_coupon);
            // this.ratingBar1 = (RatingBar) itemView
            // .findViewById(R.id.ratingBar1);
            iv_back.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mOnHairStyleClickListener.onBackClick();
                }
            });
            iv_collection.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mOnHairStyleClickListener.onCollectionClick(
                            HairStylePublicAdapter.this.id,
                            HairStylePublicAdapter.this.isCollected);
                }
            });
//            iv_appointment.setOnClickListener(new OnClickListener() {
//
//                @Override
//                public void onClick(View v) {
//                    // TODO Auto-generated method stub
//                    mOnHairStyleClickListener
//                            .onAppointmentClick(stylistID);
//                }
//            });
            iv_share.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    mOnHairStyleClickListener
                            .onShareClick(HairStylePublicAdapter.this.id, HairStylePublicAdapter.this.mainPhoto);
                }
            });
            // btn_coupon.setOnClickListener(new OnClickListener() {
            //
            // @Override
            // public void onClick(View v) {
            // // TODO Auto-generated method stub
            // try {
            // mOnHairStyleClickListener
            // .onCouponClick(HairStyleAdapter.this.coupon
            // .getString("id"));
            // } catch (JSONException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // }
            // }
            // });
        }

    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_hairstyle;

        public RecyclerViewHolder(View view) {
            super(view);
            // implementing onClickListener
            // iv_hairstyle = (ImageView) view.findViewById(R.id.iv_hairstyle);
            iv_hairstyle = (ImageView) view.findViewById(R.id.iv_style_photo);

        }
    }

    public class TitleViewHolder extends RecyclerView.ViewHolder {


        public TitleViewHolder(View view) {
            super(view);

        }
    }

    public class DreserListViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_dresser;
        TextView tv_name;
        TextView tv_post;
        TextView tv_orders;
        TextView tv_item;
        TextView tv_price;
        LinearLayout ll_appointment;

        public DreserListViewHolder(View view) {
            super(view);
            iv_dresser = (ImageView) view.findViewById(R.id.iv_dresser);
            iv_dresser.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    JSONObject stylist = getDresserObj(getAdapterPosition());
                    if (stylist != null) {
                        String id = CommonUtils.getJsonString(stylist, "id");
                        HairDresserDetailActivity_.intent(mContext).extra("id",id).flags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).start();
                    }




                }
            });
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_post = (TextView) view.findViewById(R.id.tv_post);
            tv_orders = (TextView) view.findViewById(R.id.tv_orders);
            tv_item = (TextView) view.findViewById(R.id.tv_item);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            ll_appointment = (LinearLayout) view.findViewById(R.id.ll_appointment);

        }
    }

    public class MoreViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll;

        public MoreViewHolder(View view) {
            super(view);
            ll = (LinearLayout) view.findViewById(R.id.ll);
            ll.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    l.onMoreClick(view, stylistID);
                }
            });
        }
    }

}
